home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / futils / futils~1 / src / misc1s.zoo / misc1 / which.c
Encoding:
C/C++ Source or Header  |  1991-11-23  |  1.6 KB  |  64 lines

  1. /* which command - searches through path for a executable with the
  2.  * given name.
  3.  */
  4. #include <stdio.h>
  5. /* #include <memory.h> */
  6. #include <string.h>
  7. #include <types.h>
  8. #include <stat.h>
  9.  
  10. extern char *   getenv();
  11. char               filename[2048];
  12.  
  13. main(argc, argv)
  14.    int             argc;
  15.    char *          argv[];
  16. {
  17.    int             i,pathlen,sufflen,filelen;
  18.    char            * path;
  19.    register char   * pathtok;
  20.    register char   * curpath;
  21.    char            * suff;
  22.    register char   * cursuff;
  23.    struct stat     statb;
  24.  
  25.    path = getenv("PATH");
  26.    pathlen = strlen(path);
  27.    pathtok = malloc(pathlen);
  28.    suff = ".ttp,.tos,.prg,.sh,.g";
  29.    sufflen = strlen(suff);
  30.  
  31.  
  32.    for (i = 1; i < argc; ++i) {
  33.       memcpy(pathtok,path,pathlen);
  34.       curpath=strtok(pathtok,":");
  35.       while (curpath!=(char *)NULL) {
  36.  
  37.          cursuff=suff;
  38.          while(*cursuff!='\0') {
  39.             strcpy(filename,curpath);
  40.             strcat(filename,"/");
  41.             strcat(filename,argv[i]);
  42.             filelen=strlen(filename);
  43.             while (*cursuff!='\0' && *cursuff!=',') {
  44.                filename[filelen]=*cursuff;
  45.                filelen++;
  46.                cursuff++;
  47.             }
  48.             filename[filelen]='\0';
  49.             if (stat(filename,&statb)==0) {
  50.                printf("%s\n",filename);
  51.                goto next_argument;
  52.             }
  53.             if (*cursuff!='\0') {
  54.                cursuff++;
  55.             }
  56.          } /* next suffix */
  57.          curpath=strtok(NULL,":");
  58.       } /* next path */
  59.       printf("%s not found in %s\n",argv[i],path);
  60. next_argument:;
  61.    } /* next argument */
  62.    return 0;
  63. }
  64.